<?php
/**
 * @package Joomla
 * @subpackage Jmonoslideshow
 * @copyright (C) 2010 - Matthieu BARBE - www.ccomca.com
 * @license GNU/GPL v2
 * 
 * Jmonoslideshow is a Joomla extension for Monoslideshow.
 *
 * Jmonoslideshow uses :
 * Monoslideshow (http://www.monoslideshow.com/)
 * Plupload (http://www.plupload.com/) by Moxicode Systems
 * json class by Michal Migurski, Matt Knapp, Brett Stimmerman
 * jquery a JavaScript Library (http://jquery.com/)
 * getid3 library (http://getid3.sourceforge.net/)
 * swfobject is an easy-to-use and standards-friendly method to embed Flash content, which utilizes one small JavaScript file (http://code.google.com/p/swfobject/)
 * Chained Selects jQuery Plugin (http://www.appelsiini.net/2010/jquery-chained-selects)
 *
 * Jmonoslideshow is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
 

class MediaHelper
{
	
	/**
	 * Checks if the file is an image
	 * @param string The filename
	 * @return boolean
	 */
	function isImage( $fileName )
	{
		static $imageTypes = 'xcf|odg|gif|jpg|png|bmp';
		return preg_match("/$imageTypes/i",$fileName);
	}
	
	/**
	 * Get an extension authorize
	 *
	 * @return string (extension)
	 */
	function extension_authorize ()
	{
		$params = &JComponentHelper::getParams( 'com_jmonoslideshow' );
		$ext = "jpg,png,jpeg,gif";
		if ($params->get( 'authorize_swf', 1) == 1) $ext .= ",swf";
		if ($params->get( 'authorize_video', 1) == 1) $ext .= ",f4v,flv";
		return $ext;
	}
	
	/**
	 * Get a type media
	 *
	 * @param string extension media
	 * @return int
	 */
	function getType ($extension)
	{
		if ($extension == "swf")
			{
				$type = 1;
			}
			else if ($extension == "f4v" || $extension == "flv")
			{
				$type = 2;
			}
			else if ($extension == "jpg" || $extension == "png" || $extension == "jpeg" || $extension == "gif")
			{
				$type = 0;
			}
			else
			{
				$type = 3;
			}
			
			return $type;
	}
	
	/**
	 * Get a extension file
	 *
	 * @param string extension media
	 * @return string
	 */
	function getExtension ($extension)
	{
		$type = MediaHelper::getType($extension);
		if ($type == 1)
			{
				$extension = "swf";
			}
			else if ($type == 2)
			{
				$extension = $extension;
			}
			else if ($type == 0)
			{
				$extension = $extension;
			}
			else
			{
				$extension = "rss";
			}
			
			return $extension;
	}
	
	/**
	 * Get a type name
	 *
	 * @param int type
	 * @return string
	 */
	function getTypeName ($type)
	{
		
		if ($type == 1)
			{
				$name = "COM_JMONOSLIDESHOW_FLASH";
			}
			else if ($type == 2)
			{
				$name = "COM_JMONOSLIDESHOW_VIDEO";
			}
			else
			{
				$name = "COM_JMONOSLIDESHOW_IMAGE";
			}
			
			return $name;
	}
	
	/**
	 * Get a info media
	 *
	 * @param int type
	 * @param int id
	 * @return string
	 */
	function getInfoMedia ($type, $id, $extension)
	{
		
		if ($type == 1 || $type == 2)
			{
				$info = "";
			}
			else
			{
				$params = &JComponentHelper::getParams( 'com_jmonoslideshow' );
				$info = "<img src='".JURI::root()."images/monoslideshow/images/".$id.".".$extension."' width='".$params->get( 'width_image_hover', 400 )."' />";
			}
			
			return $info;
	}


	function countFiles( $dir )
	{
		$total_file = 0;
		$total_dir = 0;

		if (is_dir($dir)) {
			$d = dir($dir);

			while (false !== ($entry = $d->read())) {
				if (substr($entry, 0, 1) != '.' && is_file($dir . DIRECTORY_SEPARATOR . $entry) && strpos($entry, '.html') === false && strpos($entry, '.php') === false) {
					$total_file++;
				}
				if (substr($entry, 0, 1) != '.' && is_dir($dir . DIRECTORY_SEPARATOR . $entry)) {
					$total_dir++;
				}
			}

			$d->close();
		}

		return array ( $total_file, $total_dir );
	}
	
	/**
	 * Get a info media ID tag
	 *
	 * @param int type
	 * @param string file
	 * @return array
	 */
	
	function info_media ($type, $file)
	{
		require_once JPATH_COMPONENT_ADMINISTRATOR.DS.'library'.DS.'getid3'.DS.'getid3.php';
		
		// Needed for windows only
		define('GETID3_HELPERAPPSDIR', 'C:/helperapps/');
		$getID3 = new getID3;
		$getID3->setOption(array('encoding' => 'UTF-8'));
		$file = JPATH_ROOT.DS."media".DS."com_jmonoslideshow".DS."upload".DS.$file;
		
		if (GETID3_OS_ISWINDOWS) {
		
		// this mostly just gives a consistant look to Windows and *nix filesystems
		// (windows uses \ as directory seperator, *nix uses /)
		$file = str_replace('\\', '/', $file);
		}
		
		
		$ThisFileInfo = $getID3->analyze($file);
		getid3_lib::CopyTagsToComments($ThisFileInfo);
		//print_r ($ThisFileInfo);
		return @$ThisFileInfo;
	}
	
	/**
	 * Get a max size image in pixel
	 * @return int
	 */
	
	function getSizeImageLimit ()
	{
		$memorylimit = ini_get('memory_limit');
		$size = ($memorylimit*1000000) / 5;
		return round(sqrt($size));
	}
	
	function getThumb ($filename, $thumbname = false, $id = false)
	{
		
		require_once JPATH_COMPONENT_ADMINISTRATOR.DS.'library'.DS.'ThumbLib.inc.php';
		$params = &JComponentHelper::getParams( 'com_jmonoslideshow' );
		$maxThumbnailWidth = $params->get('width_vignette', '120');
		$maxThumbnailHeight = $params->get( 'height_vignette', '90' );
		$base_upload_dir = JPATH_ROOT.DS . 'media' . DS . 'com_jmonoslideshow' . DS . 'upload' ;
		$jpgQuality = $params->get( 'qualtiy_image', '75' );
		$options = array('jpegQuality' => $jpgQuality);
		$name = "";
		
		if (($filename && $type == 0) && $thumbname == "")
				{
					if ( JFile::exists($base_upload_dir.'/'.$filename) && !JFile::exists($base_upload_dir.'/'.$maxThumbnailWidth.'-'.$maxThumbnailHeight.'/'.$filename) )
						{
							$thumb = PhpThumbFactory::create($base_upload_dir.DS.''.$filename, $options);
							$thumb->resize($maxThumbnailWidth, $maxThumbnailHeight);
							$thumb->save($base_upload_dir.'/'.$maxThumbnailWidth.'-'.$maxThumbnailHeight.'/'.$filename);
							$name = $filename;
						}
						$name = $filename;
				}
				else
				{
					if ( JFile::exists($base_upload_dir.'/'.$thumbname) && !JFile::exists($base_upload_dir.'/'.$maxThumbnailWidth.'-'.$maxThumbnailHeight.'/'.$thumbname) )
						{
							$thumb = PhpThumbFactory::create($base_upload_dir.DS.''.$thumbname, $options);
							$thumb->resize($maxThumbnailWidth, $maxThumbnailHeight);
							$thumb->save($base_upload_dir.'/'.$maxThumbnailWidth.'-'.$maxThumbnailHeight.'/'.$thumbname);
							
						}	
						$name = $thumbname;
				}
		$rel = '';
		if ($id) $rel = 'rel="'.JURI::root().'administrator/index.php?option=com_jmonoslideshow&task=media.getMediaIntormation&format=raw&id='.$id.'"';
  		if ($name) return '<img src="'.JURI::root().'media/com_jmonoslideshow/upload/'.$maxThumbnailWidth.'-'.$maxThumbnailHeight.'/'.$name.'" '.$rel.' class="infomedia"/>'; else return 'error';
	}

}